package com.project.dao.impl;

import com.project.bean.AssessBean;
import com.project.bean.UserBean;
import com.project.dao.IAssessDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class AssessDaoImpl extends BaseDao implements IAssessDao {
    @Override
    public void add(AssessBean assess) {
        this.setConnection();
        try {
            ps=con.prepareStatement("insert into t_assess(fk_userId,fk_goodsId,t_content) values(?,?,?) ");
            ps.setObject(1,assess.getUser().getId());
            ps.setObject(2,assess.getGoods().getId());
            ps.setObject(3,assess.getContent());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public List<AssessBean> findByProduct(int productId) {
        this.setConnection();
        List<AssessBean> list = new ArrayList<>();
        try {
            ps=con.prepareStatement("SELECT s.*, p.`t_minName` FROM t_user p,t_assess s WHERE p.`pk_userId`=s.`fk_userId` AND s.`fk_goodsId`=?;");
            ps.setObject(1,productId);
            rs = ps.executeQuery();
            while (rs.next()){
                AssessBean assess = new AssessBean();
                assess.setContent(rs.getString("t_content"));
                UserBean user = new UserBean();
                user.setMinName(rs.getString("t_minName"));
                assess.setUser(user);
                list.add(assess);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
